package com.ca.ui.core.view.card;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.ca.ui.core.R;
import com.ca.ui.core.util.NumberUtils;
import com.ca.ui.core.util.ScoreUtils;
import com.ca.ui.core.view.bar.MiniLineProcessBar;
import com.ca.ui.core.view.bar.MiniScoreLineProcessBar;
import com.ca.ui.core.view.bar.MiniScoreProcessBar;
import com.ca.ui.core.view.button.RoundButton;

import java.util.ArrayList;
import java.util.List;

public class ScoreCard extends ConstraintLayout {

    private static final int MAX_SCORE = 5;

    private float advScore;
    private int postScoreCount;
    private int wantCount;
    private int grade1Count;
    private int grade2Count;
    private int grade3Count;
    private int grade4Count;
    private int grade5Count;

    public TextView labelVIew;
    public TextView scoreView;
    public MiniScoreProcessBar scoreProcessBar;
    public TextView postScoreCountView;

    private final List<MiniScoreLineProcessBar> lineProcessBarList = new ArrayList<>();

    public TextView wantCountView;
    public Button wantButton;
    public Button postScoreButton;

    public ScoreCard(@NonNull Context context) {
        super(context);
        initView(context, null);
    }

    public ScoreCard(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public ScoreCard(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {

        View view = LayoutInflater.from(context).inflate(R.layout.score_card_layout, this);

        labelVIew = view.findViewById(R.id.tv_label);
        scoreView = view.findViewById(R.id.tv_score);
        scoreProcessBar = view.findViewById(R.id.bar_miniScoreProcess);
        postScoreCountView = view.findViewById(R.id.tv_postScoreCount);
        wantCountView = view.findViewById(R.id.tv_wantCount);

        lineProcessBarList.add(view.findViewById(R.id.mslpb_1));
        lineProcessBarList.add(view.findViewById(R.id.mslpb_2));
        lineProcessBarList.add(view.findViewById(R.id.mslpb_3));
        lineProcessBarList.add(view.findViewById(R.id.mslpb_4));
        lineProcessBarList.add(view.findViewById(R.id.mslpb_5));

        wantButton = view.findViewById(R.id.btn_want);
        postScoreButton = view.findViewById(R.id.btn_postScore);

        int color = getResources().getColor(R.color.primary);
        wantButton.setBackgroundColor(color);
        postScoreButton.setBackgroundColor(color);

        if (attrs != null) {
            TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ScoreCard, 0, 0);

            advScore = array.getFloat(R.styleable.ScoreCard_sc_advScore, 0);
            postScoreCount = array.getInteger(R.styleable.ScoreCard_sc_postScoreCount, 0);
            wantCount = array.getInteger(R.styleable.ScoreCard_sc_wantCount, 0);
            grade1Count = array.getInteger(R.styleable.ScoreCard_sc_grade1Count, 0);
            grade2Count = array.getInteger(R.styleable.ScoreCard_sc_grade2Count, 0);
            grade3Count = array.getInteger(R.styleable.ScoreCard_sc_grade3Count, 0);
            grade4Count = array.getInteger(R.styleable.ScoreCard_sc_grade4Count, 0);
            grade5Count = array.getInteger(R.styleable.ScoreCard_sc_grade5Count, 0);

            array.recycle();
        }

        setAdvScore(advScore);
        setPostScoreCount(postScoreCount);
        setWantCount(wantCount);
        setGrade1Count(grade1Count);
        setGrade2Count(grade2Count);
        setGrade3Count(grade3Count);
        setGrade4Count(grade4Count);
        setGrade5Count(grade5Count);
    }

    public float getAdvScore() {
        return advScore;
    }

    public void setAdvScore(float advScore) {
        this.advScore = advScore;
        String text = ScoreUtils.scoreToString(advScore);
        this.scoreView.setText(text);
        this.scoreProcessBar.setScore((int) advScore);
    }

    public int getPostScoreCount() {
        return postScoreCount;
    }

    public void setPostScoreCount(int postScoreCount) {
        this.postScoreCount = postScoreCount;
        String text = NumberUtils.numberToString((long) postScoreCount) + "位车主打分";
        this.postScoreCountView.setText(text);
    }

    public int getWantCount() {
        return wantCount;
    }

    public void setWantCount(int wantCount) {
        this.wantCount = wantCount;
        String text = NumberUtils.numberToString((long) wantCount) + "人想买";
        this.wantCountView.setText(text);
    }

    public int getGrade1Count() {
        return grade1Count;
    }

    public void setGrade1Count(int grade1Count) {
        this.grade1Count = grade1Count;
        MiniScoreLineProcessBar bar = get(0);
        bar.setValue(this.grade1Count);
        bar.setRatio((float) this.grade1Count / (float) this.postScoreCount);
    }

    public int getGrade2Count() {
        return grade2Count;
    }

    public void setGrade2Count(int grade2Count) {
        this.grade2Count = grade2Count;
        MiniScoreLineProcessBar bar = get(1);
        bar.setValue(this.grade2Count);
        bar.setRatio((float) this.grade2Count / (float) this.postScoreCount);
    }

    public int getGrade3Count() {
        return grade3Count;
    }

    public void setGrade3Count(int grade3Count) {
        this.grade3Count = grade3Count;
        MiniScoreLineProcessBar bar = get(2);
        bar.setValue(this.grade3Count);
        bar.setRatio((float) this.grade3Count / (float) this.postScoreCount);
    }

    public int getGrade4Count() {
        return grade4Count;
    }

    public void setGrade4Count(int grade4Count) {
        this.grade4Count = grade4Count;
        MiniScoreLineProcessBar bar = get(3);
        bar.setValue(this.grade4Count);
        bar.setRatio((float) this.grade4Count / (float) this.postScoreCount);
    }

    public int getGrade5Count() {
        return grade5Count;
    }

    public void setGrade5Count(int grade5Count) {
        this.grade5Count = grade5Count;
        MiniScoreLineProcessBar bar = get(4);
        bar.setValue(this.grade5Count);
        bar.setRatio((float) this.grade5Count / (float) this.postScoreCount);
    }

    public MiniScoreLineProcessBar get(int index) {
        return lineProcessBarList.get(index);
    }
}
